package com.lanou3g.k12.web.controller;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lanou3g.k12.entity.Carousel;
import com.lanou3g.k12.entity.CouponRecord;
import com.lanou3g.k12.service.CarouselService;
import com.lanou3g.k12.utils.K12Result;
import com.lanou3g.k12.utils.K12ResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//轮播图管理 carousel-controller
@RestController
public class CarouselController {

    @Autowired
    public CarouselService carouselService;

    /**
     * 添加轮播图信息方法
     *
     * @param carousel 前端传 需要添加的轮播图信息
     * @return 状态码
     */
    @PostMapping("/carousel")
    public K12Result addCarouse(Carousel carousel) {
        Boolean src = carouselService.add(carousel);
        if (src) {
            return K12ResultUtils.ok(200);
        } else {
            return K12ResultUtils.error(1400);
        }
    }

    /**
     * 获取全部轮播图
     *
     * @param page 页数
     * @param size 显示条数
     * @return K12Result 状态码 所有轮播图信息
     */
    @GetMapping("/carousels")
    public K12Result findCarousel(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                  @RequestParam(value = "size", defaultValue = "10") Integer size) {
        Page<Object> objects = PageHelper.startPage(page, size);
        carouselService.findAll();
        return K12ResultUtils.ok(objects);
    }

    /**
     * 通过id查询对应轮播图
     *
     * @param id 前端传 需要查的轮播图id
     * @return 成功返回 查询结果   失败返回1404
     */
    @GetMapping("/carousel/{id}")
    public K12Result findById(@PathVariable Integer[] id) {
        List<Carousel> list = carouselService.carouselById(id);
        if (list.size() > 0) {
            return K12ResultUtils.ok(list);
        } else {
            return K12ResultUtils.ok(1404);
        }
    }

    /**
     * 通过id 修改轮播图信息
     *
     * @param id       被修改的轮播图id
     * @param carousel 被修改的参数
     * @return 返回成功或者失败状态码
     */
    @PutMapping("/carousel/{id}")
    public K12Result update(@PathVariable Integer id, Carousel carousel) {
        Boolean str = carouselService.updateById(id, carousel);
        if (str) {
            return K12ResultUtils.ok(200);
        } else {
            return K12ResultUtils.error(1404);
        }
    }

    /**
     * 通过id删除轮播图
     *
     * @param id 被删除的id
     * @return 返回成功或者失败状态码
     */
    @DeleteMapping("/carousel/{id}")
    public K12Result delete(@PathVariable Integer id) {
        System.out.println("进入方法");
        int i = carouselService.delById(id);
        System.out.println("影响条数: " + i);
        if (i == 1) {
            return K12ResultUtils.ok(200);
        } else {
            return K12ResultUtils.error(1404);
        }
    }


    /**
     * 通过id 多个删除
     *
     * @param ids 接收多个被删除的id
     * @return 返回成功或者失败状态码
     */
    @DeleteMapping("/carousels/{ids}")
    public K12Result deletes(@PathVariable Integer[] ids) {
        int i = 0;
        for (Integer id : ids) {
            int i1 = carouselService.delById(id);
            i += i1;
        }
        if (i == ids.length) {
            return K12ResultUtils.ok(200);
        } else {
            return K12ResultUtils.error(1400);
        }
    }


    /**
     * * 首页轮播图提取方法
     *
     * @return 对应数量轮播图
     */
    @GetMapping("/carousels/show")
    public K12Result showCarousel() {
        List<Carousel> carousels = carouselService.showBySort();
        if (carousels != null) {
            return K12ResultUtils.ok(carousels);
        } else {
            return K12ResultUtils.error(1404);
        }
    }




}
